package LW.org.mainTest;

import LW.org.model.Admin;
import net.sf.json.JSONArray;
import org.apache.commons.collections.map.HashedMap;
import org.hibernate.*;
import LW.org.model.Room;
import LW.org.model.RoomUse;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class mainTest {

    @SuppressWarnings("unchecked")
    public static void main(String[] args) {


//通过sql语句获取行数，通过行数来确定插入是否成功
//        ApplicationContext cfg = new ClassPathXmlApplicationContext("applicationContext.xml");
//        SessionFactory sessionFactory = (SessionFactory)cfg.getBean("sessionFactory");
//        Session session = sessionFactory.openSession();
//        Transaction tx = session.beginTransaction();
//
//        SQLQuery query = session.createSQLQuery("select count(*) from [hotel].[dbo].hotel_room_use");
//        System.out.println(query.list().get(0));

//        select();
//
//
        ApplicationContext cfg = new ClassPathXmlApplicationContext("applicationContext.xml");
        SessionFactory sessionFactory = (SessionFactory)cfg.getBean("sessionFactory");
        Session session = sessionFactory.openSession();
        Transaction tx = session.beginTransaction();

        int index = 0;
        Map allTest = new HashedMap();
        Query query = session.createQuery("from RoomUse r");
//        query.setParameter("id",1);
        List<RoomUse> all = query.list();
        for(RoomUse single : all) {

//            Map map = new HashMap();
//            map.put("hotelInfoId",single.getHotelInfoId());
//            map.put("hotelClientName",single.getHotelClientName());
//            map.put("hotelClientIdcard",single.getHotelClientIdcard());
//            map.put("hotelClientTel",single.getHotelClientTel());
//            map.put("roomId",single.getRoom().getRoomId());
//            map.put("hotelInTime",single.getHotelInTime().toString());
//            System.out.println(single.toString());
//            allTest.put(index+"",map);
//            index++;
        }
        JSONArray json = new JSONArray().fromObject(allTest);
        System.out.println("****"+json);
        tx.commit();
        session.close();




    }

    public static void select(){
        ApplicationContext cfg = new ClassPathXmlApplicationContext("applicationContext.xml");
        SessionFactory sessionFactory = (SessionFactory)cfg.getBean("sessionFactory");
        Session session = sessionFactory.openSession();
        Transaction tx = session.beginTransaction();

        Query query = session.createQuery("from RoomUse");
        List<RoomUse> all = query.list();
        for(RoomUse a : all)
        {
            System.out.println(a.toString());
        }
        tx.commit();
        session.close();
    }

    public static void selectAllRoom()
    {
        ApplicationContext cfg = new ClassPathXmlApplicationContext("applicationContext.xml");
        SessionFactory sessionFactory = (SessionFactory)cfg.getBean("sessionFactory");
        Session session = sessionFactory.openSession();
        Transaction tx = session.beginTransaction();

        Query query = session.createQuery("from Room");
        List<Room> all = query.list();
        for(Room a : all)
        {
            System.out.println(a.toString());
        }
        tx.commit();
        session.close();
    }
}
